package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.Dish;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        log.info("购物车数据:: {}",shoppingCart);
        Long currentId = BaseContext.getCurrentId();

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one != null){
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        }else{
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }

        return R.success(shoppingCart);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,currentId);
        qw.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(qw);

        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,currentId);

        shoppingCartService.remove(qw);
        return R.success("成功清空");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        if (shoppingCart.getDishId() != null){
            LambdaQueryWrapper<ShoppingCart> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            qw1.eq(ShoppingCart::getUserId,currentId);
            ShoppingCart shoppingCartOne = shoppingCartService.getOne(qw1);
            if (shoppingCartOne.getNumber() > 1){
                int number = shoppingCartOne.getNumber() - 1;
                shoppingCartOne.setNumber(number);
                shoppingCartService.updateById(shoppingCartOne);
            }else {
                shoppingCartService.removeById(shoppingCartOne);
            }

        }else{
            LambdaQueryWrapper<ShoppingCart> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            qw1.eq(ShoppingCart::getUserId,currentId);
            ShoppingCart shoppingCartOne = shoppingCartService.getOne(qw1);
            if (shoppingCartOne.getNumber() > 1){
                int number = shoppingCartOne.getNumber() - 1;
                shoppingCartOne.setNumber(number);
                shoppingCartService.updateById(shoppingCartOne);
            }else {
                shoppingCartService.removeById(shoppingCartOne);
            }

        }

        return R.success("成功修改");

    }





}
